#include "AudioVideoAutomation.h"

#include "IF_MediaEngineInterface.h"
#include "AudioVideoSource.h"
#include "MediaListModel.h"
#include "ActionListController.h"
#include "Fader.h"
#include "ApplicationSettings.h"
#include "testableAssert.h"

AudioVideoAutomation::AudioVideoAutomation( IF_MediaEngineInterface *engine, MediaListModel *model,
                                            ActionListController *controller, Fader *fader,
                                            ApplicationSettings & applicationsettigns,
                                            QObject *parent) :
   QObject( parent),
   m_engine( engine),
   m_model( model),
   m_controller( controller),
   m_fader( fader),
   m_currentSource( NULL),
   m_applicationsettigns( applicationsettigns)
{
   connect( m_engine, SIGNAL(finished()), this, SLOT(onTrackFinished()) );
   connect( m_fader, SIGNAL(changeVolume(int)), m_engine, SLOT(setVolume(int)) );

   m_defaultVolume = m_applicationsettigns.defaultVolume();
}

/**
 * Active current index may change for different reasons: a new track
 * selected, tracks above deleted without changing the active one, or
 * active track deleted itself.
 */
void AudioVideoAutomation::activateByRowNumer( int activeRow)
{
   /* check if a track has been activated */
   if (activeRow >= 0)
   {
      /* valid track activated */
      QModelIndex index = m_model->index(activeRow);
      const AbstractMediaSource *source = m_model->mediaData( index);

      /* if source is not audio/video, this will be NULL */
      const AudioVideoSource * avSource = dynamic_cast<const AudioVideoSource *>(source);
      applyMediaSource( avSource);
   }
   else
   {
      /* no active entry: it might have been erased */
      m_engine->stop();
   }
}


void AudioVideoAutomation::activateById(const QString &stringId, bool playImmediately)
{
   QModelIndex target = m_model->searchById( stringId);

   T_ASSERT_REP (target != QModelIndex(),
                 QString(tr("No media has name: ")) + stringId);

   m_controller->activateRequest( target.row());

   if (playImmediately)
   {
      m_engine->play();
   }
}


void AudioVideoAutomation::applyMediaSource(const AudioVideoSource * audioSource)
{
   if (audioSource != m_currentSource)
   {
      /* track effectively changed. */
      m_currentSource = audioSource;

      m_engine->pause();
      m_engine->setCurrentSource( m_currentSource );
   }
}

/**
 * start or stop volume fading.
 */
void AudioVideoAutomation::toggleFader()
{
   m_fader->toggleFrom( m_engine->volume());
}

/**
 * actions taken when media functionality is about to be closed
 */
void AudioVideoAutomation::onClosing()
{
   m_engine->stop();
   m_applicationsettigns.setDefaultVolume( m_defaultVolume);
}

/**
 * set audio volume to default value. If a fade is in progress,
 * it is blocked.
 */
void AudioVideoAutomation::applyDefaultVolume()
{
   m_fader->stop();
   m_engine->setVolume( m_defaultVolume);
}

/**
 * When one track is over, next one is activated without playing
 */
void AudioVideoAutomation::onTrackFinished()
{
   int activeRow = m_controller->activeItemRow();

   if ((activeRow != ActionListController::NoItemActive) &&
       (activeRow < (m_model->rowCount() - 1)))
   {
      m_controller->activateRequest( activeRow + 1);
   }
}

